package net.luoran.dvm.controller;

import net.luoran.dvm.common.annotation.PassToken;
import net.luoran.dvm.common.base.Result;
import net.luoran.dvm.common.util.AuthUtil;
import net.luoran.dvm.entity.SysAuthTokenDO;
import net.luoran.dvm.service.SysAuthTokenService;
import net.luoran.dvm.service.SysUserService;
import net.luoran.dvm.service.UserContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

/**
 * @author ：M1nG
 * @date ：Created in 2020/11/9 11:34
 * @description：
 * @version: 1.0
 */
@Controller
@RequestMapping("/show")
@PassToken
public class DvsController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserContentService userContentService;

    @Autowired
    private SysAuthTokenService sysAuthTokenService;


    @RequestMapping("/expire")
    public String expire() {
        return "expire";
    }

    /*@GetMapping("/getToken/{id}")
    public Result getAuthtoken(@PathVariable("id") String id) {
        Result result = Result.ok();
        QueryWrapper<SysAuthTokenDO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return result;
    }
*/
    @RequestMapping("/getToken")
    @ResponseBody
    public Result getToken(@RequestParam("key") String key) {
        Result result = Result.ok();
        try {
            SysAuthTokenDO authToken = sysAuthTokenService.getById(key);
            if (!LocalDateTime.now().isAfter(authToken.getExpireTime())) {
                return result.build("403", "授权已过期！");
            }
            result.setData(authToken.getToken());
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }


    @RequestMapping("/authToken")
    @ResponseBody
    public Result authToken(@RequestParam("token") String token) {
        Result result = Result.ok();
        try {
            if (!AuthUtil.verify(token)) {
                result = Result.build("403", "授权已过期或授权码已经失效！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("系统错误");
        }
        return result;
    }


}
